using System;
using System.Collections.Generic;
using Data;
using TMPro;
using UnityEngine;

public class PictureWidgetHandler : MonoBehaviour, IWidget, IHandlerBase
{
	public void SetData(GameObject _parent, Package _package, Level _level)
	{
		this.parent = _parent;
		this.newIcon.SetActive(false);
		this.picturepackage = _package;
		this.picturelevel = _level;
		if (this.picturelevel.record.isClear)
		{
			this.collider.enabled = true;
			this.levelText.gameObject.SetActive(false);
			if (this.picturelevel.record.isNewCollection)
			{
				this.newIcon.SetActive(true);
			}
			this.LoadLevel();
			this.locked.SetActive(false);
		}
		else
		{
			this.collider.enabled = false;
			this.levelText.gameObject.SetActive(true);
			this.levelText.text = string.Format("{0}-{1}", this.picturepackage.display, this.picturelevel.num + 1);
			this.locked.SetActive(true);
		}
	}

	public void DoDataExchange()
	{
	}

	private void LoadLevel()
	{
		this.ClearBoxWidget();
		this.ClearHinderWidget();
		GameMode.gameMode = (GameType)this.picturelevel.mode;
		if (GameMode.gameMode == GameType.TRIANGLE)
		{
			BlocksCoordinate.coordinate = new BlockTriangleCoordinate();
		}
		else if (GameMode.gameMode == GameType.SQUARE)
		{
			BlocksCoordinate.coordinate = new BlockSquareCoordinate();
		}
		else if (GameMode.gameMode == GameType.HEXA)
		{
			BlocksCoordinate.coordinate = new BlockHexaCoordinate();
		}
		else if (GameMode.gameMode == GameType.RHOMBUS)
		{
			BlocksCoordinate.coordinate = new BlockRhombusCoordinate();
		}
		else if (GameMode.gameMode == GameType.TANGRAM)
		{
			BlocksCoordinate.coordinate = new BlockTangramCoordinate();
		}
		BlocksCoordinate.coordinate.coordinate.width = this.picturelevel.width;
		BlocksCoordinate.coordinate.coordinate.height = this.picturelevel.height;
		float num = this.top.transform.position.y - this.bottom.transform.position.y;
		float num2 = 0f;
		if (GameMode.gameMode == GameType.HEXA || GameMode.gameMode == GameType.RHOMBUS)
		{
			num2 = BlocksCoordinate.coordinate.coordinate.SizeY / 2f;
		}
		BlocksCoordinate.coordinate.coordinate.size = BlocksCoordinate.coordinate.defaultCoordinate.size;
		if (BlocksCoordinate.coordinate.coordinate.SizeY * (float)this.picturelevel.height + num2 > num)
		{
			BlocksCoordinate.coordinate.coordinate.size = num / (float)this.picturelevel.height / (BlocksCoordinate.coordinate.coordinate.SizeY / BlocksCoordinate.coordinate.coordinate.size) - num2 / (float)this.picturelevel.height;
		}
		float num3 = (float)this.picturelevel.width;
		if (GameMode.gameMode == GameType.TANGRAM)
		{
			num3 += 2f;
		}
		else if (GameMode.gameMode == GameType.TRIANGLE)
		{
			num3 += 1f;
		}
		else if (GameMode.gameMode == GameType.RHOMBUS)
		{
			num3 *= 0.6666667f;
		}
		float num4 = Mathf.Abs(this.left.transform.localPosition.x) * 2f;
		if (BlocksCoordinate.coordinate.coordinate.SizeX * num3 > num4)
		{
			BlocksCoordinate.coordinate.coordinate.size = num4 / num3 / (BlocksCoordinate.coordinate.coordinate.SizeX / BlocksCoordinate.coordinate.coordinate.size);
		}
		this.GenerateBoard();
		this.GeneratePosition();
	}

	private void GenerateBoard()
	{
		int num = 0;
		for (int i = 0; i < this.picturelevel.blockcount; i++)
		{
			for (int j = 0; j < this.picturelevel.level[i].Length; j++)
			{
				Point point = BlocksCoordinate.coordinate.coordinate.IndexToPuzzle((int)this.picturelevel.level[i][j]);
				bool isUp;
				if (this.picturelevel.isReverse)
				{
					isUp = ((point.x + point.y) % 2 != 0);
				}
				else
				{
					isUp = ((point.x + point.y) % 2 == 0);
				}
				Vector3 localPosition = BlocksCoordinate.coordinate.coordinate.PuzzleToWorld((float)point.x, (float)point.y);
				localPosition.z = 0f;
				PictureBoxWidgetHandler item = Widget.Create<PictureBoxWidgetHandler>();
				this.boxWidgets.Add(item);
				localPosition.z = -2f;
				this.boxWidgets[num].transform.parent = this.boardBg.transform;
				this.boxWidgets[num].transform.localPosition = localPosition;
				this.boxWidgets[num].transform.localScale = Vector3.one * BlocksCoordinate.coordinate.coordinate.size;
				this.boxWidgets[num].tag = "Untagged";
				int index = i;
				if (this.picturelevel.changeColor != null)
				{
					index = (int)this.picturelevel.changeColor[i][j];
				}
				int tangramIndex = (point.x + (int)this.picturelevel.editMinPoint[0] + (((point.y + (int)this.picturelevel.editMinPoint[1]) % 2 != 0) ? 2 : 0)) % 4;
				this.boxWidgets[num].SetData(index, isUp, tangramIndex, true);
				this.boxWidgets[num].name = "box";
				num++;
			}
		}
		if (this.picturelevel.hinder != null)
		{
			for (int k = 0; k < this.picturelevel.hinder.Length; k++)
			{
				Point point2 = BlocksCoordinate.coordinate.coordinate.IndexToPuzzle((int)this.picturelevel.hinder[k]);
				bool isUp2;
				if (this.picturelevel.isReverse)
				{
					isUp2 = ((point2.x + point2.y) % 2 != 0);
				}
				else
				{
					isUp2 = ((point2.x + point2.y) % 2 == 0);
				}
				Vector3 localPosition2 = BlocksCoordinate.coordinate.coordinate.PuzzleToWorld((float)point2.x, (float)point2.y);
				localPosition2.z = 0f;
				HinderWidgetHandler item2 = Widget.Create<HinderWidgetHandler>();
				this.hinderWidgets.Add(item2);
				this.hinderWidgets[k].transform.parent = this.boardBg.transform;
				this.hinderWidgets[k].transform.localPosition = localPosition2;
				this.hinderWidgets[k].transform.localScale = Vector3.one * BlocksCoordinate.coordinate.coordinate.size;
				this.hinderWidgets[k].name = "hinder";
				int tangramIndex2 = (point2.x + (int)this.picturelevel.editMinPoint[0] + (((point2.y + (int)this.picturelevel.editMinPoint[1]) % 2 != 0) ? 2 : 0)) % 4;
				this.hinderWidgets[k].SetData(isUp2, tangramIndex2);
			}
		}
	}

	private void GeneratePosition()
	{
		float num = 9999f;
		float num2 = -9999f;
		for (int i = 0; i < this.boxWidgets.Count; i++)
		{
			if (this.boxWidgets[i].transform.position.y < num)
			{
				num = this.boxWidgets[i].transform.position.y;
			}
			if (this.boxWidgets[i].transform.position.y > num2)
			{
				num2 = this.boxWidgets[i].transform.position.y;
			}
		}
		for (int j = 0; j < this.hinderWidgets.Count; j++)
		{
			if (this.hinderWidgets[j].transform.position.y < num)
			{
				num = this.hinderWidgets[j].transform.position.y;
			}
			if (this.hinderWidgets[j].transform.position.y > num2)
			{
				num2 = this.hinderWidgets[j].transform.position.y;
			}
		}
		Vector3 position = base.transform.position;
		float num3 = (num - position.y + (num2 - position.y)) / 2f;
		this.boardBg.transform.localPosition = new Vector3(this.boardBg.transform.localPosition.x, this.boardBg.transform.localPosition.y - num3, this.boardBg.transform.localPosition.z);
	}

	private void ClearBoxWidget()
	{
		for (int i = 0; i < this.boxWidgets.Count; i++)
		{
			UnityEngine.Object.Destroy(this.boxWidgets[i].gameObject);
		}
		this.boxWidgets.Clear();
	}

	private void ClearHinderWidget()
	{
		for (int i = 0; i < this.hinderWidgets.Count; i++)
		{
			UnityEngine.Object.Destroy(this.hinderWidgets[i].gameObject);
		}
		this.hinderWidgets.Clear();
	}

	public void OnClick_Self(tk2dUIItem obj)
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		this.parent.SendMessage("OnClickPicture", this);
	}

	public BoxCollider collider;

	public GameObject top;

	public GameObject bottom;

	public GameObject left;

	public GameObject boardBg;

	public GameObject locked;

	public TextMeshPro levelText;

	public GameObject newIcon;

	private GameObject parent;

	public Package picturepackage;

	public Level picturelevel;

	private List<HinderWidgetHandler> hinderWidgets = new List<HinderWidgetHandler>();

	private List<PictureBoxWidgetHandler> boxWidgets = new List<PictureBoxWidgetHandler>();
}
